package queue;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * Controller to start the queue in a separate thread and to handle user events
 * from the QueueGUI class
 * 
 * @author Andrew Stokes
 * 
 */
public class QueueController {

	private Queue queue;
	private QueueGUI queueGUI;

	public QueueController(Queue queue, QueueGUI queueGUI) {
		this.queue = queue;

		this.queueGUI = queueGUI;
		queueGUI.addBuildQueueButtonListener(new BuildQueueButtonListener());
		queueGUI.addQueueSpeedSliderListener(new QueueSpeedSliderListener());

	}

	public class BuildQueueButtonListener implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			queueGUI.setBuildQueueButtonEnabled(false);
			Thread thread = new Thread(queue);
			thread.start();

		}
	}

	/**
	 * 
	 * @author Andrew
	 * 
	 *         listener for the queue speed slider, sets the speed with which
	 *         the queue fills up with passengers
	 */
	public class QueueSpeedSliderListener implements ChangeListener {

		public void stateChanged(ChangeEvent e) {
			JSlider source = (JSlider) e.getSource();
			if (!source.getValueIsAdjusting()) {

				// set the speed of the queue
				queue.setQueueSpeed(source.getValue());
			}

		}
	}

}
